<template>
	<div id="school_year_add_edit" class="dialog animated" :class="open? 'fadeIn':'fadeOut'" v-show="show">
		<div class="panel animated" :class="open? 'zoomIn':''">
			<div class="title">
				{{isAdd? '新增学年' : '编辑学年'}}
				<div class="close" @click="cancel()"></div>
			</div>
			<div class="form">
				<div class="row">
					<label class="label">
						<span>*</span>
						选择学年
					</label>
					<el-date-picker v-model="param.year" id="year" type="year" :picker-options="pickerOptions"
				      placeholder="" style="width: 2rem;" :clearable="false" value-format="yyyy">
				    </el-date-picker>
				    &nbsp;
				    <span>{{param.name}}</span>
				</div>
				<div class="row">
					<label class="label">
						<span>*</span>
						学年起止时间
					</label>
					<el-date-picker v-model="param.startTime" id="startTime" value-format="yyyy-MM-dd"
				      	placeholder="" style="width: 2rem;" :clearable="false" type="date">
				    </el-date-picker>
				    &nbsp;
				    <el-date-picker v-model="param.endTime" id="endTime" value-format="yyyy-MM-dd"
				      placeholder="" style="width: 2rem;" :clearable="false" type="date">
				    </el-date-picker>
				</div>
				<div class="row">
					<label class="label">
						<span>*</span>
						上学期起止时间
					</label>
					<el-date-picker v-model="param.firstStartTime" id="firstStartTime" value-format="yyyy-MM-dd"
				      placeholder="" style="width: 2rem;" :clearable="false" type="date">
				    </el-date-picker>
				    &nbsp;
				    <el-date-picker v-model="param.firstEndTime" id="firstEndTime" value-format="yyyy-MM-dd"
				      placeholder="" style="width: 2rem;" :clearable="false" type="date">
				    </el-date-picker>
				</div>
				<div class="row">
					<label class="label">
						<span>*</span>
						下学期起止时间
					</label>
					<el-date-picker v-model="param.secondStartTime" id="secondStartTime" value-format="yyyy-MM-dd"
				      placeholder="" style="width: 2rem;" :clearable="false" type="date">
				    </el-date-picker>
				    &nbsp;
				    <el-date-picker v-model="param.secondEndTime" id="secondEndTime" value-format="yyyy-MM-dd"
				      	placeholder="" style="width: 2rem;" :clearable="false" type="date">
				    </el-date-picker>
				</div>
				<div class="row">
					<label class="label">
						是否当前学年
					</label>
					<el-checkbox v-model="param.isCurSchoolYear">是</el-checkbox>
				</div>
				
			</div>
			<div class="btn_div">
				<div class="btn_sure" @click="sure()">确 认</div>
				<div class="btn_cancel" @click="cancel()">取 消</div>
			</div>
		</div>
	
	</div>
</template>

<script>
var vm,dict;
var param = {
	year : "",
	name : "",
	startTime : "",
	endTime : "",
	firstStartTime : "",
	firstEndTime : "",
	secondStartTime : "",
	secondEndTime : "",
	isCurSchoolYear : false,
	campusId : "",
	_wait : ""
}
export default {
	data : function() {
		return {
			show : false,
			open : false,
			param : tool.clone(param),
			schoolYear: null,
			year : "",
			isAdd : true,
			schoolYearList : null,
			pickerOptions: {},
			curSchoolYear: null, //是否已有当前学年
		}
	},
	watch : {
		"param.year" : function() {
			let y1 = this.param.year;
			if(y1) {
				let y2 = parseInt(y1) + 1;
				this.param.name = y1 + "-" + y2 + "学年";
			}
		}
	},
	methods : {
		init : function(schoolYear) {
			vm.show = true;
			vm.open = true;
			
			vm.isAdd = (schoolYear? false : true);
			//初始化数据
			vm.loadList();
			
			
			//有传收费类型对象,则是编辑,否则新增
			if(schoolYear) {
				vm.schoolYear = schoolYear;
				vm.param = tool.clone(schoolYear);
				vm.param.isCurSchoolYear = (vm.param.isCurSchoolYear=="1" ? true : false);
				vm.param._wait = "";
			} else {
				vm.param = tool.clone(param);
				vm.param.campusId = vm.$store.state.campusId;
			}
		},
		cancel : function() {
			vm.open = false;
			setTimeout(function() {
				vm.show = false;
			},500);
		},
		sure : function() {
			
			let p = tool.clone(vm.param);
			//当前学年的两个年份数组
			let yearList = [p.year, (parseInt(p.year) + 1 + "")];
			//验证规则
			var rules = [{
				judge : p.year,
				focus : $("#school_year_add_edit #year"),
				tips : "请输入1-20位由字母汉字数字（）_-组成的名称"
			},{
				judge : yearList.indexOf(p.startTime.substring(0,4)) >= 0,
				focus : $("#school_year_add_edit #startTime"),
				tips : "学年开始时间不在" + p.name + "时间范围内"
			},{
				judge : yearList.indexOf(p.endTime.substring(0,4)) >= 0,
				focus : $("#school_year_add_edit #endTime"),
				tips : "学年结束时间不在" + p.name + "时间范围内"
			},{
				judge : vm.param.startTime < vm.param.endTime,
				focus : $("#school_year_add_edit #endTime"),
				tips : "学年结束时间须大于学年开始时间"
			},{
				judge : (p.startTime <= p.firstStartTime) && (p.firstStartTime <= p.endTime),
				focus : $("#school_year_add_edit #firstStartTime"),
				tips : "上学期开始时间不在学年起止时间范围内"
			},{
				judge :  (p.startTime <= p.firstEndTime) && (p.firstEndTime <= p.endTime),
				focus : $("#school_year_add_edit #firstEndTime"),
				tips : "上学期结束时间不在学年起止时间范围内"
			},{
				judge : vm.param.firstStartTime < vm.param.firstEndTime,
				focus : $("#school_year_add_edit #firstEndTime"),
				tips : "上学期结束时间须大于上学期开始时间"
			},{
				judge : (p.startTime <= p.secondStartTime) && (p.secondStartTime <= p.endTime),
				focus : $("#school_year_add_edit #secondStartTime"),
				tips : "下学期开始时间不在学年起止时间范围内"
			},{
				judge : (p.startTime <= p.secondEndTime) && (p.secondEndTime <= p.endTime),
				focus : $("#school_year_add_edit #secondEndTime"),
				tips : "下学期结束时间不在学年起止时间范围内"
			},{
				judge : vm.param.firstEndTime < vm.param.secondStartTime,
				focus : $("#school_year_add_edit #secondStartTime"),
				tips : "下学期开始时间须大于上学期结束时间"
			},{
				judge : vm.param.secondStartTime < vm.param.secondEndTime,
				focus : $("#school_year_add_edit #secondEndTime"),
				tips : "下学期结束时间须大于下学期开始时间"
			},{
				judge : !(vm.curSchoolYear && p.isCurSchoolYear), //已经有了当前学年,这里还设置当前学年
				tips : vm.curSchoolYear + "已为当前学年，不得重复设置，请先变更"
			}];
			
			if(!tool.validate(rules)) {
				return;
			}
			
			//转换勾选值格式
			p.isCurSchoolYear = (p.isCurSchoolYear ? "1" : "0");
			
			if(vm.isAdd) {
				//新增
				api.schoolYearAdd(p).then(function(data) {
					tool.message({showClose: true, message: "新增年级成功", type: 'success'});
					vm.$emit('callback');
				    vm.cancel();
				});
			} else {
				//编辑
				api.schoolYearEdit(p).then(function(data) {
					tool.message({showClose: true, message: "编辑年级成功", type: 'success'});
					vm.$emit('callback');
				    vm.cancel();
				});
			}
			
		},
		loadList : function() {
			let campusId = vm.$store.state.campusId;
			//查询所有已有的学年
			api.schoolYearAll({campusId}).then(function(data) {
				vm.schoolYearList = data.list;
				//获取已有的学年列表
				let yearList = [];
				//清空当前学年,重新设
				vm.curSchoolYear = null;
				data.list.forEach(function(item) {
					//如果是编辑,则排除自身
					if(!vm.isAdd && vm.schoolYear.schoolYearId == item.schoolYearId) {
						return;
					}
					yearList.push(item.year);
					//已有当前学年
					if(item.isCurSchoolYear == "1") {
						vm.curSchoolYear = item.name;	
					}
				});
				vm.pickerOptions = {
					disabledDate(time) {
						//禁止选择已有学年
						time = tool.DateToString(time, "yyyy");
			           return yearList.indexOf(time)>=0;
			        }
				}
				
			});
		}
	},
	created : function() {
		vm = this;
//		vm.init();
	}
}
</script>

<style lang="scss">
#school_year_add_edit {
	.el-input__inner {
		border-radius: 0.08rem;
		font-size: 0.16rem;
		color: #666666;
		height: 0.36rem;
		line-height: 0.36rem;
	}
	.el-checkbox__label {
		font-size: 0.16rem;
		color: #666666;
	}
}
</style>
<style scoped src="@/style/module/dialog.css"></style>
<style scoped>
	
	.panel {
		width: 7rem;
	}
	.form {
		padding: 0 0.3rem;
		font-size: 0.16rem;
		color: #666666;
	}
	.row {
		line-height: 0.5rem;
	}
	.label {
		width: 1.4rem;
		display: inline-block;
		text-align: right;
		margin-right: 0.15rem;
		vertical-align: top;
	}
	.label>span {
		color: red;
		display: inline-block;
		width: 0.1rem;
		position: relative;
		top: 0.02rem;
	}
	.explain {
		color: #999999;
		font-size: 0.14rem;
		line-height: 0.2rem;
		padding-left: 1.1rem;
		padding-bottom: 0.08rem;
	}
</style>